# Standard libraries
import wsgiref.handlers
# AppEngine imports
from google.appengine.ext import webapp
from google.appengine.api import users

from db_model import PostalCode

class Admin:
  def getPostalCodes(self):
    suburbs = []
    for postal_code in PostalCode.all():
      suburbs.append(postal_code.suburb)
    return suburbs

class AdminServer(webapp.RequestHandler):
  """Handles requests to /admin URLs and delegates to the Admin class."""
  def get(self):
    """Invokes methods from the Admin class based on the 'action' parameter"""
    admin = Admin()
    action = self.request.get('action')
    if action == 'init':
      admin.initGifts()
      admin.initGiftTransactions()
      msg = "Gifts have been initialized, gift transactions have been cleared."
      self.response.out.write(msg)
    elif action == 'list':
      self.response.out.write("Postal Codes = %s" % admin.getPostalCodes())
    else:
      html = []
      html.append('<a href="/admin?action=init">Initialize datastore</a><br>')
      html.append('<a href="/admin?action=list">List all data in datastore</a>')
      self.response.out.write(''.join(html))


